/*
 * $Id: ObjectForm.java 112 2009-07-01 11:33:10Z donbeave $
 */

package labour.manager.client.widget.form;

import labour.concept.client.data.BaseObject;
import labour.concept.client.data.ObjectType;

import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;

public class ObjectForm extends FormPanel {

  private LabelField idField;
  private ComboBox<ObjectType> typesBox;
  private LabelField dtUpdateField;
  private LabelField parentIdField;
  private LabelField childsCountField;

  public ObjectForm() {
    setHeading("Object");

    setFrame(true);

    idField = new LabelField();
    idField.setFieldLabel("ID");
    add(idField);

    typesBox = new ComboBox<ObjectType>();
    typesBox.setFieldLabel("Type");
    typesBox.setDisplayField("Name");
    add(typesBox);

    dtUpdateField = new LabelField();
    dtUpdateField.setFieldLabel("Date Update");
    add(dtUpdateField);

    parentIdField = new LabelField();
    parentIdField.setFieldLabel("Parent ID");
    add(parentIdField);

    childsCountField = new LabelField();
    childsCountField.setFieldLabel("Children count");
    add(childsCountField);
  }

  public ObjectForm(BaseObject object) {
    this();

    setItem(object);
  }

  public LabelField getIdField() {
    return idField;
  }

  public ComboBox<ObjectType> getTypesBox() {
    return typesBox;
  }

  public LabelField getDateUpdateField() {
    return dtUpdateField;
  }

  public LabelField getParentIdField() {
    return parentIdField;
  }

  public LabelField getChildrenCountField() {
    return childsCountField;
  }

  public Object getItem() {
    return getData("MODEL");
  }

  public void setItem(BaseObject item) {
    setData("MODEL", item);

    getIdField().setValue(item.getModel().getId());
    getParentIdField().setValue(item.getModel().getParentId());
    getChildrenCountField().setValue(item.getModel().getChildrenCount());

    ListStore<ObjectType> types = new ListStore<ObjectType>();
    types.add(item.getManager().getObjectsTypes());

    getTypesBox().setStore(types);

    getTypesBox().setValue(item.getType());
  }

}
